import ThreeRender from "./ThreeRender";
import ThreeScene from './ThreeScene.js'
import ThreeCamera from './ThreeCamera'
import ThreeGrid from "./ThreeGrid";
import FactoryGeometry from "./factory/FactoryGeometry";
import FactoryMaterial from "./factory/FactoryMaterial";
import UtilRandom from "../../../util/ToolRandom";
import ThreeRaycaster from "./ThreeRaycaster";
import ThreeRain from './ThreeRain'
import TestLoader from "./loader/TestLoader";
import ThreeSpriteText from "./ThreeSpriteText";
import FactoryText from "./factory/FactoryText";
import * as THREE from "three";
// import geometry from "./factory/FactoryGeometry";
import Stats from "../../../components/flush-stats";
let dom
let scene;
let camera;

let obj3ds = [
    // new THREE.Mesh(FactoryGeometry.box, FactoryMaterial.three),
    // new THREE.Mesh(FactoryGeometry.sphere, FactoryMaterial.one),
    createLine(),
]

function init(_dom) {
    dom = _dom

    Stats.flush(_dom)

    // 场景初始化
    scene = ThreeScene.initDefault()

    console.log(_dom.clientHeight)
    // 相机初始化
    camera = ThreeCamera.initCamera(75, _dom.clientWidth / _dom.clientHeight, 0.1, 3000);
    ThreeScene.add(camera)

    // 坐标系初始化 - 参数设置了三条轴线的长度
    ThreeScene.add(new THREE.AxesHelper(2000));

    // 地面网格初始化
    ThreeScene.add(ThreeGrid.init());

    // 初始化环境光
    ThreeScene.add(new THREE.AmbientLight(0x444444))

    // 初始化点光源
    ThreeScene.add(initPoint())

    // 渲染器初始化
    dom.appendChild(ThreeRender.initRender(_dom.clientWidth, _dom.clientHeight, scene, camera))

    // 初始化选择器
    ThreeRaycaster.init(scene, camera, dom)

    // 添加场景内容
    obj3ds.forEach(obj => {
        ThreeScene.add(obj)
    })

    // 添加3D模型
    setTimeout(() => {
        TestLoader.loaderModel(function (obj) {
            console.log(obj)
            ThreeScene.add(obj)
        })
    }, 1000)
}

/**
 * 光源设置
 */
function initPoint() {
    // 点光源
    let point = new THREE.PointLight(0xffffff);
    point.position.set(0, 5000, 0); //点光源位置
    return point;
}

/**
 * 创建3d线条
 */
function createLine() {
    // 划线的材质
    let lineMaterial = new THREE.LineBasicMaterial( { color: 0x0000ff } );
    // 创建线坐标
    let lineGeometry = new THREE.BufferGeometry();

    lineGeometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array([
        -20, 0, 0,
        0, 20, 0,
        20, 0, 0
    ]), 3))

    let line = new THREE.Line( lineGeometry, lineMaterial );

    setInterval(() => {
        line.rotateY(0.03)
    }, 30)

    return line;
}

let _index = 0;

function randomElement(geometry = FactoryGeometry.getByIndex(UtilRandom.randomNum(0, 5))) {
    // 网格
    let cube = new THREE.Mesh(geometry, FactoryMaterial.getByIndex(UtilRandom.randomNum(0, 2)));

    cube.name = _index++ + '号对象'

    cube.position.set(UtilRandom.randomNum(-500, 500), UtilRandom.randomNum(0, 500), UtilRandom.randomNum(-500, 500));

    return ThreeScene.add(cube);
}

function openRain() {
    ThreeRain.init(scene)
}

function closeRain() {
    ThreeRain.close()
}

function addText(text) {
    let textObj = ThreeSpriteText.makeTextSprite(text,
        {
            fontsize: 20,
            borderColor: { r:255, g:0, b:0, a:0.4 },/* 边框黑色 */
            backgroundColor: { r:255, g:255, b:255, a:0.9 }/* 背景颜色 */
        });
    textObj.position.set(200, 200, 200)
    ThreeScene.add(textObj)
}

function add3DText(text) {
    FactoryText.createText(text).then(geometry => {
        randomElement(geometry);
    })
}

export default {
    init,
    randomElement,
    openRain,
    closeRain,
    addText,
    add3DText
}
